/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-14 23:18:33
 */
import {
  AmbientLight,
  DirectionalLight,
  DoubleSide,
  MeshLambertMaterial,
  Vector3,
} from "three";
import ThreeBase from "three-base";
import threeUtils from "three-base/threeUtils";

import MeshBase from "three-base/MeshBase";
import Ground from "./component3d/Ground/Ground";
import Sky from "./component3d/Sky/Sky";
import Grass from "./component3d/Grass/Grass";

/**
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {
  constructor() {
    super();
  }

  intensity = 1.5;

  //环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight("#ffffff", this.intensity);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //周围的平行光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight("#ffffff", this.intensity);
    directionalLight.position.set(10, 1, -10);
    directionalLight.castShadow = true;
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  directionalLight2 = (() => {
    const directionalLight2 = new DirectionalLight("#ffffff", this.intensity);
    directionalLight2.position.set(10, 1, -10);
    directionalLight2.castShadow = true;
    this.scene.add(directionalLight2);
    return directionalLight2;
  })();

  //草地
  grass = (() => {
    const grass = new Grass();
    this.add(grass);
    return grass;
  })();

  sky = (() => {
    const sky = new Sky();
    this.scene.add(sky);
    return sky;
  })();

  //机场
  airport = (() => {
    const airport = new MeshBase("./airport.glb");
    const material = new MeshLambertMaterial({
      side: DoubleSide,
    });
    airport.traverseMesh((mesh) => {
      mesh.geometry.computeVertexNormals();
      mesh.material = material;
      mesh.castShadow = true;
      mesh.receiveShadow = true;
    });
    airport.addEventListener("loaded", () => {
      //   threeUtils.center(airport);
    });
    this.add(airport);
    return airport;
  })();

  init() {
    super.init();
    this.axesHelper.visible = false;
    this.gridHelper.visible = false;
    this.controls.screenSpacePanning = false;
    this.renderer.shadowMap.enabled = true; // 支持阴影

    this.resetLookAt({
      center: new Vector3(112.582, 0, 59.417),
      position: new Vector3(166.851, 52.483, 32.727),
    });
  }

  render() {
    super.render();
  }
}

const homeRenderer = (window.homeRenderer = new HomeRender());

export default homeRenderer;
